package com.py.framework.controller;

import com.py.framework.common.Constants;
import com.py.framework.common.controller.SystemController;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.entity.UserEntity;
import com.py.framework.entity.WorkFlowOutGoing;
import com.py.framework.entity.WorkFlowWrapper;
import com.py.framework.service.ActReviewService;
import com.py.framework.service.AuthenticationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程审核Controller
 *
 * @author Leegern
 * @date 2018年5月23日
 */
@RestController
@RequestMapping("/act")
public class ActReviewController extends SystemController {

    @Autowired
    private ActReviewService actReviewService;
    @Autowired
    private AuthenticationService authenticationService;

    /**
     * @api {post} /act/queryActReviewDataList.json 分页查询工作流程审核列表
     * @apiName queryActReviewDataList
     * @apiVersion 0.0.1
     * @apiGroup 流程审核
     * @apiParam {int} page 当前页码
     * @apiParam {int} limit 每页条数
     * @apiParam {HttpServletRequest} request 请求对象
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} busName 业务名称
     * @apiSuccess {String} processDefinitionName 流程名称
     * @apiSuccess {String} taskName 任务名称
     * @apiSuccess {String} createTime 创建时间
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":null,"count":2,"data":[{"taskId":"100","busName":"审批","processDefinitionName":"请假","taskName":"信用中心","createTime":"2018-04-27
     * 14:33:00"}]}
     */
    @RequestMapping(value = "/queryActReviewDataList", method = RequestMethod.POST)
    public RestServiceResult<List<WorkFlowWrapper>> queryActReviewDataList(int page, int limit, String startTime,
                                                                           String endTime, String processDefinitionName, HttpServletRequest request) {
        long userId = super.pickUserId(request);
        Page pager = super.getPage(page, limit);
        List<WorkFlowWrapper> datas = null;
        if (userId > Constants.NUMBER_ZERO) {
            datas = actReviewService.queryActReviewDataList(pager, userId, startTime, endTime, processDefinitionName);
        }
        return super.getPageSuccessRestServiceResult(pager.getTotalCount(), datas);
    }

    /**
     * @api {post} /act/queryActReviewById.json 根据业务Id获取工作流程审核数据
     * @apiName queryActReviewById
     * @apiVersion 0.0.1
     * @apiGroup 流程审核
     * @apiParam {long} businessId 业务Id
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccessExample {json} 成功返回结果 {}
     */
    @RequestMapping(value = "/queryActReviewById", method = RequestMethod.POST)
    public RestServiceResult<Object> queryActReviewById(long businessId) {
        Object data = null;
        if (businessId > Constants.NUMBER_ZERO) {
            data = actReviewService.queryActReviewById(businessId);
        }
        return super.getSuccessRestServiceResult(data);
    }

    /**
     * @api {post} /act/getHistoryComment.json 获取历史批注
     * @apiName getHistoryComment
     * @apiVersion 0.0.1
     * @apiGroup 流程审核
     * @apiParam {String} procId 流程实例Id
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} taskName 任务名称
     * @apiSuccess {String} userName 审核人员
     * @apiSuccess {String} comment 审核意见
     * @apiSuccess {String} time 审核时间
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":null,"count":2,"data":[{"taskName":"上报","userName":"管理员","comment":"同意","time":"2018-04-27
     * 14:33:00"}]}
     */
    @RequestMapping(value = "/getHistoryComment", method = RequestMethod.POST)
    public RestServiceResult<Object> getHistoryComment(String procId) {
        Object data = null;
        if (StringUtils.isNotBlank(procId)) {
            data = actReviewService.getHistoryComment(procId);
        }
        return super.getSuccessRestServiceResult(data);
    }

    /**
     * @api {post} /act/getActNextNode.json 获取审核流程中下一步流程走向
     * @apiName getActNextNode
     * @apiVersion 0.0.1
     * @apiGroup 流程审核
     * @apiParam {String} taskId 任务Id
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} activityName 下一节点名称
     * @apiSuccess {String} candidateGroup 下一节点参与人
     * @apiSuccess {String} outGoing 下一流向名称
     * @apiSuccess {String} type 下一节点类型描述
     * @apiSuccess {OutGoingCondition} condition 节点表达式
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":null,"count":2,"data":[{"activityName":"上报","candidateGroup":"管理员","outGoing":"管理员审批","type":"审批","condition":{"paramName":"userId","paramValue":"1001"}}]}
     */
    @RequestMapping(value = "/getActNextNode", method = RequestMethod.POST)
    public RestServiceResult<List<WorkFlowOutGoing>> getActNextNode(String taskId) {
        List<WorkFlowOutGoing> datas = null;
        if (StringUtils.isNotBlank(taskId)) {
            datas = actReviewService.getActNextNode(taskId);
        }
        return super.getSuccessRestServiceResult(datas);
    }

    /**
     * @api {post} /act/examineTask.json 用户审批
     * @apiName examineTask
     * @apiVersion 0.0.1
     * @apiGroup 流程审核
     * @apiParam {HttpServletRequest} request 请求对象
     * @apiParam {String} taskId 任务Id
     * @apiParam {String} procId 流程实例Id
     * @apiParam {long} userId 用户Id
     * @apiParam {String} paramName 条件名称
     * @apiParam {String} paramValue 条件值
     * @apiParam {String} comment 批注
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} result
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":null,"count":0,"data":null}
     */
    @RequestMapping(value = "/examineTask", method = RequestMethod.POST)
    public RestServiceResult<Object> examineTask(HttpServletRequest request,
                                                 @RequestParam(value = "taskId", required = true) String taskId,
                                                 @RequestParam(value = "procId", required = true) String procId,
                                                 @RequestParam(value = "userId", required = false, defaultValue = "0") long userId,
                                                 @RequestParam(value = "paramName", required = false) String paramName,
                                                 @RequestParam(value = "paramValue", required = false) String paramValue,
                                                 @RequestParam(value = "comment", required = false) String comment) {
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        String jdToken = HttpServletHelper.getUserToken(request);
        if (StringUtils.isNotBlank(taskId) && StringUtils.isNotBlank(procId)) {
            try {
                // 用户信息
                UserEntity user = authenticationService.getCurrentUserInfo(jdToken);
                if (null != user && user.getId() > Constants.NUMBER_ZERO) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("taskId", taskId);
                    params.put("procId", procId);
                    params.put("userId", userId);
                    params.put("paramName", paramName);
                    params.put("paramValue", paramValue);
                    params.put("comment", comment);
                    params.put("currUserId", user.getId());
                    params.put("currRealName", user.getRealName());
                    actReviewService.examineTask(params);
                }
            } catch (Exception e) {
                result = super.getFailedRestServiceResult("审核失败！");
                ExceptionLogger.error(e);
            }
        }
        return result;
    }

    /**
     * @api {post} /act/getFlowChart.json 获取流程图
     * @apiName getFlowChart
     * @apiVersion 0.0.1
     * @apiGroup 流程审核
     * @apiParam {String} executionId 活动的流程Id
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":null,"count":0,"data":"/images/100001.jpg"}
     */
    @RequestMapping(value = "/getFlowChart", method = RequestMethod.GET)
    public void getFlowChart(String executionId) {

        actReviewService.getFlowChart(executionId);

    }
}